﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;

namespace Factory
{
    class BancoDAO
    {

        public static IList<Banco> obtemBancos()
        {
            try
            {
                DAOBase m_cnn = new DAOBase();

                #region Query
                string query =
                    @"SELECT idBanco, nome from Banco";
                #endregion

                SqlCommand cmd = new SqlCommand(query, m_cnn.CreateConnection());

                SqlDataReader dr = cmd.ExecuteReader();

                IList<Banco> banco = ReaderToObjects(dr);

                return banco;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return null;
            }
        }

        public static IList<Banco> ReaderToObjects(IDataReader reader)
        {
            if (reader.IsClosed)
            {
                throw new ArgumentException("O DataReader fornecido já estava fechado.");
            }

            int Id = reader.GetOrdinal("IDBanco");
            int nome = reader.GetOrdinal("Nome");
          
            IList<Banco> result = new List<Banco>();

            while (reader.Read())
            {
                Banco b = new Banco();

                b.id = Convert.ToString(reader.GetInt32(Id));
                b.nome = reader.GetString(nome);
                result.Add(b);
            }
            return result;
        }

    }
}
